﻿using AntMovement.Core.IParameter;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using AntMovement.Menu.HardControl.Model;
using AntMovement.Core.Tool;
using AntMovement.Core;
using static System.Formats.Asn1.AsnWriter;
using AntMovement.Core.Model;
using MaterialDesignThemes.Wpf;
using static AntMovernent.SolidTechESerialCard.SLDMV;
using AntMovement.Parameter.Model;
using AntMovement.WPF.Resources.Controls;
using AntMovement.Core.Domain;

namespace AntMovement.Menu.HardControl.ViewModels
{
    public class AirCylinderHmiViewModel
    {
        public AsyncObservableCollection<BackOrientationModel> PointPositions { get; set; }
        public TemporaryParameter TP { get; set; }
        public HoldParameter Hold { get; set; }
        private string _path = System.IO.Path.Combine(Environment.CurrentDirectory, "init", "palletizing");

        public AirCylinderHmiViewModel(ITemporaryParameter temporaryParameter, IHoldParameter hmi, IDialogHost dialogHost)
        {
            
            TP = (TemporaryParameter)temporaryParameter;
            PointPositions = new AsyncObservableCollection<BackOrientationModel>(TP.BackOrientation.ToList());
            Hold = (HoldParameter)hmi;
            if (System.IO.File.Exists(_path))
            {
                string str = System.IO.File.ReadAllText(_path, Encoding.UTF8);
                if (!string.IsNullOrEmpty(str))
                {
                    try
                    {
                        Datas = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<PalletizingParameterModel>>(str, OptionsHelper.Options);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            if (Datas == null) Datas = new ObservableCollection<PalletizingParameterModel>();
            _dialogHost = dialogHost;

        }
        private IDialogHost _dialogHost;
        public ObservableCollection<PalletizingParameterModel> Datas { get; set; }

        public Prism.Commands.DelegateCommand<string> NewAdd => new Prism.Commands.DelegateCommand<string>(sender =>
        {
            if (!TP.SystemStatus.ManualOperation) { return; }
            try
            {
                if (!Datas.Any(x => x.Name == sender))
                {
                    if (Datas.Count > 20)
                    {
                        _dialogHost.DialogShow("配方数量过多，请删除一些不必要的配方.", Core.Enum.LogLevel.Warning, hostDialog: Core.Enum.PopUpType.Global);
                    }
                    else
                    {
                        Datas.Add(new PalletizingParameterModel()
                        {
                            Name = sender,
                            XCount = Hold.XCount,
                            YCount = Hold.YCount,
                            CurrentDiskPosition = Hold.CurrentDiskPosition,
                            CurrentFloors = Hold.CurrentFloors,
                            CurrentStackingColumn = Hold.CurrentStackingColumn,
                            CurrentStackingLine = Hold.CurrentStackingLine,
                            MaterialSpacingColumn = Hold.CurrentStackingLine,
                            MaterialSpacingLine = Hold.CurrentStackingLine,
                        });
                        Save();
                        _dialogHost.DialogShow($"[{sender}]配方添加成功。", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
                    }
                }
                else
                {
                    _dialogHost.DialogShow("存在相同名字的配方", Core.Enum.LogLevel.Warning, hostDialog: Core.Enum.PopUpType.Global);
                }
            }
            catch (Exception ex)
            {
                _dialogHost.DialogShow($"添加配方异常：{ex}", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
            }
        });

        public Prism.Commands.DelegateCommand<string> CalibrationCmd => new Prism.Commands.DelegateCommand<string>(sender =>
        {
            var socket = new TPSocket("192.168.39.108");
            int count = Convert.ToInt32(sender);
            float[] x_current = default;
            float[] y_current = default;
            switch (count)
            {
                case 1:
                    x_current = new float[] { 1.83F, -43.11F, -87.98F, 1.96F, -43.03F, -87.91F, 2.20F, -42.81F, -87.79F };
                    y_current = new float[] { 44.66F, 44.66F, 44.46F, 89.63F, 89.63F, 89.63F, 134.61F, 134.61F, 134.61F };
                    break;
                case 2:
                    x_current = new float[] { 46.77F, 1.76F, -43.14F, 46.64F, 1.73F, -43.29F, 46.43F, 1.53F, -43.43F };
                    y_current = new float[] { 43.73F, 43.55F, 43.35F, 88.64F, 88.65F, 88.26F, 133.72F, 133.39F, 133.39F };
                    break;
                case 3:
                    x_current = new float[] { 92.48F, 47.47F, 2.4F, 92.48F, 47.39F, 2.52F, 92.6F, 47.66F, 2.49F };
                    y_current = new float[] { 44.98F, 44.98F, 44.98F, 89.96F, 89.96F, 89.96F, 134.94F, 134.94F, 134.94F };
                    break;
            }
            var dataCalibration = socket.CBA((byte)count, x_current, y_current, 1);
            socket.Exit();
        });

        private void Save() => System.IO.File.WriteAllText(_path, System.Text.Json.JsonSerializer.Serialize(Datas, OptionsHelper.Options), Encoding.UTF8);

        public Prism.Commands.DelegateCommand<string> Load => new Prism.Commands.DelegateCommand<string>(sender =>
        {
            if (!TP.SystemStatus.ManualOperation) { return; }
            PalletizingParameterModel palletizing = Datas.FirstOrDefault(x => x.Name == sender);
            if (palletizing != null)
            {
                Hold.XCount = palletizing.XCount;
                Hold.YCount = palletizing.YCount;
                Hold.CurrentDiskPosition = palletizing.CurrentDiskPosition;
                Hold.CurrentFloors = palletizing.CurrentFloors;
                Hold.CurrentStackingColumn = palletizing.CurrentStackingColumn;
                Hold.CurrentStackingLine = palletizing.CurrentStackingLine;
                Hold.MaterialSpacingColumn = palletizing.CurrentStackingLine;
                Hold.MaterialSpacingLine = palletizing.CurrentStackingLine;
                _dialogHost.DialogShow($"[{sender}]配方加载成功。", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
            }
            else
            {
                _dialogHost.DialogShow($"[{sender}]配方加载失败,不存在该配方", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
            }
        });

        public Prism.Commands.DelegateCommand<string> Update => new Prism.Commands.DelegateCommand<string>(sender =>
        {
            if (!TP.SystemStatus.ManualOperation) { return; }
            PalletizingParameterModel palletizing = Datas.FirstOrDefault(x => x.Name == sender);
            if (palletizing != null)
            {
                palletizing.XCount = Hold.XCount;
                palletizing.YCount = Hold.YCount;
                palletizing.CurrentDiskPosition = Hold.CurrentDiskPosition;
                palletizing.CurrentFloors = Hold.CurrentFloors;
                palletizing.CurrentStackingColumn = Hold.CurrentStackingColumn;
                palletizing.CurrentStackingLine = Hold.CurrentStackingLine;
                palletizing.MaterialSpacingColumn = Hold.CurrentStackingLine;
                palletizing.MaterialSpacingLine = Hold.CurrentStackingLine;
                Save();
                _dialogHost.DialogShow($"[{sender}]配方更新成功。", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
            }
            else
            {
                _dialogHost.DialogShow($"[{sender}]配方更新失败,不存在该配方", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
            }
        });

        public Prism.Commands.DelegateCommand<string> Delect => new Prism.Commands.DelegateCommand<string>(sender =>
        {
            if (!TP.SystemStatus.ManualOperation) { return; }
            PalletizingParameterModel palletizing = Datas.FirstOrDefault(x => x.Name == sender);
            if (palletizing != null)
            {
                Datas.Remove(palletizing);
                Save();
                _dialogHost.DialogShow($"[{sender}]配方删除成功。", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
            }
            else
            {
                _dialogHost.DialogShow($"[{sender}]配方删除失败,不存在该配方", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
            }
        });
    }
}
